﻿using System;

using Jack.EntityFrameworkCore.WebAPITest.Entities;
using Jack.EntityFrameworkCore.WebAPITest.Services;

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.Extensions.DependencyInjection;

namespace Jack.EntityFrameworkCore.WebAPITest.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserModel2Controller : ControllerBase
    {
        public IUnitOfWork UnitOfWork { get; set; }
        public IEfCoreUnitOfWork<DemoDbContext> EfCoreUnitOfWork { get; set; }
        public IServiceProvider ServiceProvider { get; set; }
        //public IServiceScope ServiceScope { get; set; }

        [HttpGet]
        public void Get()
        {
            var userRepository = UnitOfWork.CreateRepository<UserModel>();
            var userRepository2 = UnitOfWork.CreateRepository<UserModel>();
            var userEfCoreRepository = EfCoreUnitOfWork.CreateEfCoreRepository<UserModel>();

            Console.WriteLine(UnitOfWork == EfCoreUnitOfWork);
            Console.WriteLine(Object.ReferenceEquals(UnitOfWork, EfCoreUnitOfWork));

            Console.WriteLine(userRepository == userEfCoreRepository);
            Console.WriteLine(Object.ReferenceEquals(userRepository, userEfCoreRepository));

            Console.WriteLine(userRepository == userRepository2);
            Console.WriteLine(Object.ReferenceEquals(userRepository, userRepository2));
        }

        [HttpGet]
        public void Get2()
        {
            EfCoreUnitOfWork<DemoDbContext> efCoreUnitOfWork1 = UnitOfWork as EfCoreUnitOfWork<DemoDbContext>;
            EfCoreUnitOfWork<DemoDbContext> efCoreUnitOfWork2 = EfCoreUnitOfWork as EfCoreUnitOfWork<DemoDbContext>;

            Console.WriteLine(efCoreUnitOfWork1.DbContext == efCoreUnitOfWork2.DbContext);
            Console.WriteLine(Object.ReferenceEquals(efCoreUnitOfWork1.DbContext, efCoreUnitOfWork2.DbContext));
        }

        [HttpGet]
        public void Get3()
        {
            IScopeService scopeService = EfCoreUnitOfWork.DbContext.GetService<IScopeService>();
            IScopeService scopeService1 = EfCoreUnitOfWork.DbContext.GetService<IScopeService>();
            IScopeService scopeService2 = ServiceProvider.GetService<IScopeService>();

            IServiceProvider serviceProvider = EfCoreUnitOfWork.DbContext.GetInfrastructure<IServiceProvider>();
            IServiceProvider serviceProvider1 = HttpContext.RequestServices;

            IScopeService scopeService3 = serviceProvider.GetService<IScopeService>();
            IScopeService scopeService4 = serviceProvider1.GetService<IScopeService>();

            //Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope


            Console.WriteLine(scopeService == scopeService1);
            Console.WriteLine(scopeService == scopeService2);
            Console.WriteLine(Object.ReferenceEquals(scopeService, scopeService1));
            Console.WriteLine(Object.ReferenceEquals(scopeService1, scopeService2));

            Console.WriteLine(scopeService3 == scopeService4);
            Console.WriteLine(Object.ReferenceEquals(scopeService4, scopeService));
        }
    }
}
